package com.sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springside.core.dao.HibernateEntityDao;
import org.springside.core.dao.Page;

import com.sy.dao.ISTestDao;
import com.sy.model.STest;

@SuppressWarnings("unchecked")
public class STestDao implements ISTestDao {

	private HibernateEntityDao<STest> hibernateEntityDao;

	/**
	 * 分页获取   
	 * start ：从那条开始
	 * pagesize：每页多少条
	 */
	public List<STest> getAllStest(final int start,final int pageSize) {
//		List<STest> list = ht.executeFind(new HibernateCallback() {
//			public Object doInHibernate(Session session)
//					throws HibernateException, SQLException {
//				Query query = session.createQuery("from "+STest.class.getName());
//				query.setFirstResult((start-1)*pageSize);
//				query.setMaxResults(pageSize);
//				List list = query.list();
//				return list;
//			}
//		});
		Page page= hibernateEntityDao.pagedQuery("from "+STest.class.getName(), start, pageSize, new Object[]{});
		return page.getItems();

	}

	/**
	 * 共有多少数据
	 */
	@Override
	public int queryCount() {
		final String hql = "select count(*) from " + STest.class.getName()
				+ " n";
		Integer resultTotal = null;
		Query query= hibernateEntityDao.createQuery(hql,new Object[]{});
		resultTotal=Integer.parseInt(query.list().get(0).toString());
		return resultTotal;
	}

	/**
	 * 通过id获取
	 */
	@Override
	public STest getSTest(String id) {
		// TODO Auto-generated method stub
		return hibernateEntityDao.get(STest.class, id);
	}

	/**
	 * 保存
	 */
	@Override
	public void saveSTest(STest st) {
		// TODO Auto-generated method stub
		hibernateEntityDao.save(st);
	}

	/**
	 * 删除
	 */
	@Override
	public void deleteSTest(String id) {
		// TODO Auto-generated method stub
		hibernateEntityDao.remove(getSTest(id));
	}

	/**
	 * 更新
	 */
	@Override
	public void updateSTest(STest st) {
		// TODO Auto-generated method stub
		hibernateEntityDao.update(st);
	}

	public HibernateEntityDao<STest> getHibernateEntityDao() {
		return hibernateEntityDao;
	}

	public void setHibernateEntityDao(HibernateEntityDao<STest> hibernateEntityDao) {
		this.hibernateEntityDao = hibernateEntityDao;
	}

	/**
	 * 通过条件 分页 
	 * start ：从那条开始
	 * pagesize：每页多少条
	 */
	@Override
	public Page getAllStestByVal(int start, int pageSize, Object[] val) {
		// TODO Auto-generated method stub
		StringBuffer sb=new StringBuffer();
		sb.append("from "+STest.class.getName()+" t ");
		sb.append(" where t.id like ?");
		Object k="%"+val[0]+"%";
		Object[] objs=new Object[1];
		objs[0]=k;
		return hibernateEntityDao.pagedQuery(sb.toString(), start, pageSize, objs);
	}

}
